package com.ankie.algorithm.knapsackproblem;

import javax.imageio.metadata.IIOMetadataFormatImpl;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;

/**
 * ModelName: 01背包问题
 *
 * @author :  ankie
 * Create at:  2020/6/30 15:13
 * Copyright: (c)2020 Ankie Cloud Inc. All rights reserved.
 * @version : 1.0
 */
public class Knapsack01 {

    public static int knapsack() {
        int n = 10, m = 0;
        int[][] f = new int[n][n];
        int[] v = new int[n];
        int[] w = new int[n];

        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                f[i][j] = f[i - 1][j];
                if (j > v[i]) {
                    f[i][j] = Math.max(f[i][j], f[i-1][j-v[i]] +w[i]);
                }
            }
        }
        int res = 0;
        for (int i = 0; i <= m; i++) {
            res = Math.max(res, f[n][i]);
        }
        return res;
    }

    public static void main(String[] args) {
        knapsack();
    }

}
/**
 * f[i][j] 表示只看前 i 个物品，总体积是 j 的情况下，总价值最大是多少
 * result = max( f[n][0~V])
 *
 * 1. 不选第 i个物品，f[i][j] = f[i - 1][j];
 * 2. 选第i个物品，   f[i][j] = f[i-1][j-v[i]] +w[i];
 * f[i][j] = max{ 1, 2 }
 * f[0][0] = 0;
 * 由数据范围反推算法复杂度以及算法内容 https://www.acwing.com/blog/content/32/
 *
 * 4 * 1000000 / 1024/ 1024 大约为4M
 */